package fr.miage.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

import fr.miage.connexion.ConnexionBD;
import fr.miage.model.User;
import fr.miage.tools.Cryptography;

public class UserDAO{
	
	private Logger logger = Logger.getRootLogger();
	
	public User getById(String id) {		
		ResultSet res = null;
		String query = "select * from user WHERE idUser="+id;
		Statement st = null;
		User user = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeQuery(query);
			if(res.next()){
				user = getUser(res);
			}else{
				return null;
			}
		} catch (SQLException ex) {
			logger.error(ex);
		} finally {
			try {
				if (res != null) {
					res.close();
				}
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		
		return user;
	}
	/**
	 * Recupere l'user grace a son login qui est son mail.
	 * @param mail login de l'user
	 * @return l'user
	 */
	public User getByMail(String mail) {		
		ResultSet res = null;
		String query = "select * from user WHERE mail='"+mail+"'";
		Statement st = null;
		User user = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeQuery(query);
			if(res.next()){
				user = getUser(res);
			}else{
				return null;
			}
		} catch (SQLException ex) {
			logger.error(ex);
		} finally {
			try {
				if (res != null) {
					res.close();
				}
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		
		return user;
	} 
	
	private User getUser(ResultSet res){
		User user = new User();
		try {
			user.setFirstName(res.getString("firstName"));
			user.setIdAdress(res.getLong("idAdress"));
			user.setMail(res.getString("mail"));
			user.setName(res.getString("name"));
			user.setPhone(res.getString("phone"));
			user.setPwd(res.getString("pwd"));
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		}
		return user;
	}

	public int addUser(User user) {
		int res = 0;
		//TODO : Create and handle exception
		//user.setPwd(Cryptography.getInstance().encryptWithAES(user.getPwd()));
		
		String query = "insert into user(mail, pwd) values ('"+user.getMail()+"', '"+user.getPwd()+"')";
		Statement st = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeUpdate(query);
			return res;
		}catch(MySQLIntegrityConstraintViolationException e){
			logger.error(e);
			e.printStackTrace();
			return -1;
		}catch (SQLException ex) {
			logger.error(ex);
			ex.printStackTrace();
			return res;
		} finally {
			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
	}

	public int deleteUser(String id) {
		int res = 0;
		String query = "delete from user where idUser="+id;
		Statement st = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeUpdate(query);
			return res;
		} catch (SQLException ex) {
			logger.error(ex);
			ex.printStackTrace();
			return res;
		} finally {
			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
	}
	
	public int updateUser(User user){
		int res = 0;		
		String query = "update user set name='"+user.getName()+"', firstName='"+user.getFirstName()+"', phone='"+user.getPhone()+"' where mail='"+user.getMail()+"'";
		Statement st = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeUpdate(query);
			return res;
		}catch (SQLException ex) {
			logger.error(ex);
			ex.printStackTrace();
			return res;
		} finally {
			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
	}

	public List<User> getAllUserByEvent(String idEvent) {
		ResultSet res = null;
		String query = "select * from invitation i inner join user u on i.idUser = u.idUser WHERE idEvent="+idEvent;
		Statement st = null;
		List<User> listeUser = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeQuery(query);
			listeUser = new ArrayList<User>();
			while(res.next()){
				User user = getUser(res);
				listeUser.add(user);
			}
		} catch (SQLException ex) {
			logger.error(ex);
		} finally {
			try {
				if (res != null) {
					res.close();
				}
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		
		return listeUser;
	}
}
